package util

import "strings"

func IsEmpty(str string) bool {
	return len(str) == 0
}
func IsIn(src []string, str string) bool {
	for _, s := range src {
		if s == str {
			return true
		}
	}
	return false
}

func Index(src []string, str string) int {
	for i, s := range src {
		if s == str {
			return i
		}
	}
	return -1
}

func Contains2(src []string, str string) bool {
	return Index(src, str) >= 0
}

// Substr returns a string of length length from the start position
func Substr(s string, start int, strlength ...int) string {
	charlist := []rune(s)
	l := len(charlist)
	length := 0
	end := 0

	if len(strlength) == 0 {
		length = l
	} else {
		length = strlength[0]
	}

	if start < 0 {
		start = l + start
	}
	end = start + length

	if start > end {
		start, end = end, start
	}

	if start < 0 {
		start = 0
	}

	if start > l {
		start = l
	}

	if end < 0 {
		end = 0
	}

	if end > l {
		end = l
	}

	return string(charlist[start:end])
}

func GetFirstBySep(src, sep string) string {
	pos := strings.Index(src, sep)
	if pos == 0 {
		return ""
	} else if pos > 0 {
		return src[0:pos]
	} else {
		return src
	}
}
